using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Rifts.Controls
{
	public class CalculatedTextBlock : Control
	{
		static CalculatedTextBlock()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(CalculatedTextBlock), new FrameworkPropertyMetadata(typeof(CalculatedTextBlock)));
		}

		#region TextWidth
		public double TextWidth
		{
			get { return (double)GetValue(TextWidthProperty); }
			set { SetValue(TextWidthProperty, value); }
		}


		public static readonly DependencyProperty TextWidthProperty =
			DependencyProperty.RegisterAttached("TextWidth", typeof(double), typeof(CalculatedTextBlock),
			new FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits, OnTextWidthChanged));

		public static void SetTextWidth(UIElement element, double value)
		{
			element.SetValue(TextWidthProperty, value);
		}

		public static double GetTextWidth(UIElement element)
		{
			return (double)element.GetValue(TextWidthProperty);
		}


		private static void OnTextWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			var var = o as CalculatedTextBlock;
			if (var != null)
				var.OnTextWidthChanged((double)e.OldValue, (double)e.NewValue);
		}



		protected virtual void OnTextWidthChanged(double oldValue, double newValue)
		{
		}

		#endregion

		#region TextHeight
		public static readonly DependencyProperty TextHeightProperty =
				DependencyProperty.RegisterAttached("TextHeight", typeof(double), typeof(CalculatedTextBlock),
					new FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits, OnTextHeightChanged));


		private static void OnTextHeightChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			var var = o as CalculatedTextBlock;
			if (var != null)
				var.OnTextHeightChanged((double)e.OldValue, (double)e.NewValue);
		}

		protected virtual void OnTextHeightChanged(double oldValue, double newValue)
		{
		}

		public double TextHeight
		{
			get { return (double)GetValue(TextHeightProperty); }
			set { SetValue(TextHeightProperty, value); }
		}

		public static void SetTextHeight(UIElement element, double value)
		{
			element.SetValue(TextHeightProperty, value);
		}

		public static double GetTextHeight(UIElement element)
		{
			return (double)element.GetValue(TextHeightProperty);
		}
		#endregion


		#region Text

		public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CalculatedTextBlock), new PropertyMetadata("", OnTextChanged));

		private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			var control = o as CalculatedTextBlock;
			if (control != null)
				control.OnTextChanged((string)e.OldValue, (string)e.NewValue);
		}
		

		protected virtual void OnTextChanged(string oldValue, string newValue)
		{

		}

		public string Text
		{
			get { return (string)GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}
		#endregion

	}
}
